package alingrad.engine.util;

public abstract class ObjectPool<T> {
	private T[] mObjects;
	private int mIndex;

	@SuppressWarnings("unchecked")
	public ObjectPool(int size) {
		mObjects = (T[])new Object[size];
		mIndex = 0;
		fill();
	}

	public T allocate() {
		assert mIndex < mObjects.length;
		assert mIndex >= 0;
		T retval = mObjects[mIndex--];
		return retval;
	}

	public void release(T object) {
		mObjects[++mIndex] = object;
	}

	public void fill() {
		for (mIndex = 0; mIndex < mObjects.length; mIndex++) {
			mObjects[mIndex] = construct();
		}
		mIndex--;
	}

	public abstract T construct();
}
